jQuery(document).ready(function($) {

	// Select shortcode
	$('#cv-generator-select').live( "change", function() {
		var queried_shortcode = $('#cv-generator-select').find(':selected').val();
		$('#cv-generator-settings').addClass('cv-loading-animation');
		$('#cv-generator-settings').load($('#cv-generator-url').val() + '/source/generator.php?shortcode=' + queried_shortcode, function() {
			$('#cv-generator-settings').removeClass('cv-loading-animation');
		});
	});

	// Insert shortcode
	$('#cv-generator-insert').live('click', function() {
		var queried_shortcode = $('#cv-generator-select').find(':selected').val();
		var cv_compatibility_mode_prefix = $('#cv-compatibility-mode-prefix').val();
		$('#cv-generator-result').val('[' + cv_compatibility_mode_prefix + queried_shortcode);
		$('#cv-generator-settings .cv-generator-attr').each(function() {
			if ( $(this).val() !== '' ) {
				$('#cv-generator-result').val( $('#cv-generator-result').val() + ' ' + $(this).attr('name') + '="' + $(this).val() + '"' );
			}
		});
		$('#cv-generator-result').val($('#cv-generator-result').val() + ']');

		// wrap shortcode
		if ( $('#cv-generator-content').val() != 'false' ) {
			$('#cv-generator-result').val($('#cv-generator-result').val() + $('#cv-generator-content').val() + '[/' + cv_compatibility_mode_prefix + queried_shortcode + ']');
		}
		window.send_to_editor(jQuery('#cv-generator-result').val());
		return false;
	});

});